<template>
  <a-modal
    :title="formTile + '标签'"
    :visible="true"
    width="60%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">
      <a-card>
        <a-row
          type="flex"
          justify="start"
          align="top"
        >
          <a-col :span="24">
            <a-form
              :form="form"
              @submit="handleSubmit"
              :labelCol="formItemLayout.labelCol"
              :wrapperCol="formItemLayout.wrapperCol"
            >
              <a-form-item
                label="标签名称"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入标签名称"
                  :maxLength="40"
                  v-decorator="[
                  `name`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入标签名称'
                      },
                      { pattern:/[^\a-\z\A-\Z0-9]/g, message: '请输入中文格式!'}
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="标签层级"
                :required="true"
              >
                <a-input-number
                  allow-clear
                  placeholder="1/2/3"
                  :min="1"
                  :max="999"
                  style="width:200px"
                  v-decorator="[
                  `indexLevel`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        message: '请输入标签层级'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="标签类型"
                :required="true"
              >
                <a-select
                  allow-clear
                  show-search
                  placeholder="请选择标签类型"
                  optionFilterProp="children"
                  v-decorator="[
                  'type',
                  {
                    rules: [{ required: true, message: '请选择标签类型!' }]
                  }
                ]"
                >
                  <a-select-option
                    :value="item.value"
                    v-for="(item,index)  in menuType"
                    :key="index"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item label="标签备注">
                <a-textarea
                  allow-clear
                  placeholder="请输入标签备注"
                  v-decorator="['remark', { rules: [{ required: true, message: '请输入标签备注' }] }]"
                ></a-textarea>
              </a-form-item>
              <a-row style="margin-top:10px">
                <a-col
                  :span="16"
                  :offset="18"
                  class="info-action"
                >
                  <a-button @click="clear">
                    重置
                  </a-button>
                  <a-button
                    :style="{ marginLeft: '8px' }"
                    type="primary"
                    html-type="submit"
                    :loading="loading"
                  >
                    保存
                  </a-button>
                </a-col>
              </a-row>
            </a-form>
          </a-col>
        </a-row>
      </a-card>
    </div>

  </a-modal>
</template>

<script>

import { postAction } from '@/api/manage'
export default {
  props: {
    account: {
      type: Object,
      default: null
    },
    appid: {
      type: String,
      default: null
    }
  },

  data() {
    return {
      urlKey: 'basemgt-wx',
      menuType:[],
      id: '',
      tagId:'',
      formItemLayout: {
        labelCol: {
          xl: { span: 5 },
          xs: { span: 5 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xl: { span: 8 },
          xs: { span: 12 },
          sm: { span: 12 }
        }
      },
      loading: false,
      form: this.$form.createForm(this),
      showTable: false
    }
  },

  created() {
    const { account } = this.$props
    if (account) {
      this.formTile = '编辑'
      this.id = account.id
      this.tagId = account.tagId
      this.$nextTick(() => {
        this.form.setFieldsValue({
          type: account.type +'',
          name: account.name,
          indexLevel: account.indexLevel,
          remark:account.remark,
        })
      })
    } else {
      this.formTile = '新增'
      this.showTable = true
    }
  },
  watch: {
    appid() {
      this.getMenuList()
    }
  },
  mounted() {
    this.getMenuList()
  },
  methods: {
    reset() {
      this.form.resetFields()
      this.$emit('close')
    },
    clear() {
      this.form.resetFields()
    },
    getMenuList() {
      const params = {
        parentId: 5
      }
      postAction(`${this.urlKey}/WxDict/findWxDictList`, params).then(data => {
        if (data.code == 200) {
          this.menuType = data.data
        } else {
          this.$message.warning(data.msg || data.message)
        }
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.submit(values)
        }
      })
    },
    submit(values) {
      this.loading = true
      var params = {
        name:values.name,
        appid: this.appid,
        type: values.type,
        indexLevel: values.indexLevel,
        remark:values.remark,
        parentId: 0,
      }
      if(this.id){
        params.tagId=this.tagId,
        params.id=this.id
      }
      const api = this.showTable ? `${this.urlKey}/WxTagList/insertWxTagList` : `${this.urlKey}/WxTagList/updateWxTagList`
      postAction(api, params).then(data => {
        if (data.code == 200) {
          this.$message.success(this.showTable ? '新建帐号成功！' : '修改成功！')
          this.reset()
        } else {
          this.$message.warning(data.data)
        }
        this.loading = false
      })
    }
  }
}
</script>


